export async function toBase64(url) {
	const res = await fetch(url);
	const blob = await res.blob();
	return new Promise(resolve => {
		const reader = new FileReader();
		reader.onloadend = () => resolve(reader.result);
		reader.readAsDataURL(blob);
	});
}


export async function replaceLottieImages(animationData, replaceMap = {}, useBase64 = true) {
	const assets = animationData.assets || [];
	for (let asset of assets) {
		const newPic = replaceMap[asset.id];
		if (!newPic) continue;


		if (useBase64) {
			const base64 = await toBase64(newPic);
			asset.e = 1;
			asset.u = "";
			asset.p = base64.replace(/^data:image\/\w+;base64,/, "");
		} else {
			asset.e = 0;
			asset.u = "";
			asset.p = newPic;
		}
	}
	return animationData;
}